package net.zjitc.service.impl;

import net.zjitc.common.Const;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.mapper.SaleMapper;
import net.zjitc.pojo.Orderitem;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.Sale;
import net.zjitc.service.SaleService;
import net.zjitc.vo.DateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @AUTHOR zhj
 * @DATE 2019/3/30 15:25
 */
@Service
@Transactional
public class SaleServiceImpl implements SaleService {
    @Autowired
    SaleMapper saleMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ProductMapper productMapper;

    /**
     * 按年月日统计
     *
     * @param vo
     * @return
     */
    @Override
    public List<Sale> findSaleInfo(DateVo vo) {
        /*如果年为空，则默认为当年*/
        if (StringUtils.isBlank(vo.getYear())) {
            Calendar date = Calendar.getInstance();
            vo.setYear(String.valueOf(date.get(Calendar.YEAR)));
        }
        if ("0".equals(vo.getMonth())) {
            vo.setMonth(null);
        }
        return saleMapper.salesRank(vo);
    }

    public List<Product> hotProducts(Integer type) {

        return null;
    }

    /**
     * 按不同的条件进行统计
     *
     * @param day
     * @return
     */
    @Override
    public List<Product> countHotSaleProducts(String day) {


        List<Product> productList = new ArrayList<>();
        List<Orders> ordersList = orderMapper.findByDayInInterval(day);
        //获取按销量排序的商品
        List<Orderitem> orderitemList = orderMapper.findItemsByOids(ordersList);
        for (Orderitem item : orderitemList) {
            Product product = productMapper.findById(item.getPid());
            productList.add(product);
        }
        if (productList.size() > 0) {
            return productList;
        }
        return null;

    }
}
